import javax.swing.*;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Properties;

public class ATMPro {
    public Properties p = new Properties();
    public String index;



    public ATMPro(){
        try {
            p.load(new FileReader("Housework0824/src/ATMPro.txt"));
        }catch (Exception e){
            System.out.println("文件不存在");
        }
    }

    //登录
    //003.pwd=123
//    003.money=2000
//    010.pwd=456
//    010.money=9000
    public void login(){
        for (int i=0;i<3;i++){
            String inCount = JOptionPane.showInputDialog(null,"输入账号");
            String inPwd = JOptionPane.showInputDialog(null,"输入密码");
            String ad = ".pwd";
            if (inPwd.equals(p.getProperty(inCount+ad))){
                JOptionPane.showMessageDialog(null,"登录成功");
                index = inCount;
                break;
            }
            JOptionPane.showMessageDialog(null,"账号或密码有误");
            if (i==2){
                JOptionPane.showMessageDialog(null,"非法用户,账号冻结");
                System.exit(0);
            }
        }
    }

    //存钱
    public void save(){
        String num = JOptionPane.showInputDialog(null,"输入存款金额");
        int now = Integer.parseInt(num);
        String a = p.getProperty(index+".money");
        now += Integer.parseInt(a);
        p.setProperty(index+".money",now+"");
        this.writer();
        this.getInform();
    }

    //取钱
    public void getMoney(){
        int get = Integer.parseInt(JOptionPane.showInputDialog(null,"输入取款金额"));
        int now = Integer.parseInt(p.getProperty(index+".money"));
        if (get>now){
            JOptionPane.showMessageDialog(null,"余额不足");
            return;
        }
        now -= get;
        p.setProperty(index+".money",now+"");
        this.writer();
        this.getInform();
    }

    //转账
    public void transMoney(){
        int number = Integer.parseInt(JOptionPane.showInputDialog(null,"输入转账金额"));
        if (number>Integer.parseInt(p.getProperty(index+".money"))){
            JOptionPane.showMessageDialog(null,"无法转账，余额不足");
            return;
        }
        String transCount = JOptionPane.showInputDialog(null,"输入汇款账号");
        if (!this.check(transCount)){
            JOptionPane.showMessageDialog(null,"汇款账户不存在");
            return;
        }
        int now = Integer.parseInt(p.getProperty(index+".money"));
        now -= number;
        int trans = Integer.parseInt(p.getProperty(transCount+".money"));
        trans += number;
        JOptionPane.showMessageDialog(null,"转账成功");
        p.setProperty(index+".money",now+"");
        p.setProperty(transCount+".money",trans+"");
        this.writer();
        this.getInform();
    }
    //判断转账账号
    public boolean check(String count){
        if (count.equals(index)){
            JOptionPane.showMessageDialog(null,"不能转账给自己");
            return false;
        }
        if (p.getProperty(count+".pwd") != null){
            return true;
        }
        return false;
    }

    //修改密码
    public void pwdChange(){
        String oldPwd = JOptionPane.showInputDialog(null,"输入旧密码");
        if (!(oldPwd.equals(p.getProperty(index+".pwd")))){
            JOptionPane.showMessageDialog(null,"旧密码输入错误");
            return;
        }
        String newPwd = JOptionPane.showInputDialog(null,"输入新密码");
        String rePwd = JOptionPane.showInputDialog(null,"确认新密码");
        if (!(newPwd.equals(rePwd))){
            JOptionPane.showMessageDialog(null,"确认密码不一致");
            return;
        }
        p.setProperty(index+".pwd",newPwd);
        this.writer();
    }


    //重写文件
    public void writer(){
        try {
            p.store(new FileWriter("Housework0824/src/ATMPro.txt"),null);
        }catch (Exception e){
            System.out.println("文件不存在");
        }
    }

    //信息查询
    public void getInform(){
        JOptionPane.showMessageDialog(null,"余额为"+p.getProperty(index+".money"));
    }









}
